//
//  CommentViewController.m
//  product_B
//
//  Created by zyc on 16/6/6.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "CommentViewController.h"
#import "RegistAndLoginViewController.h"
@interface CommentViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _comment.delegate = self;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"评论";
    _count.text = [NSString stringWithFormat:@"%d",ACCOUNT_MAX_CHARS];
    _comment.layer.cornerRadius = 5;
    _comment.layer.masksToBounds = YES;

    UIBarButtonItem *finishButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(barbuttonAction:)];
    self.navigationItem.rightBarButtonItem = finishButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)barbuttonAction:(UIBarButtonItem *)button{
    FMDBManager *manager = [FMDBManager shareInstance];
    [manager openDB];
    NSUserDefaults *defult = [NSUserDefaults standardUserDefaults];
    NSString *name = [defult objectForKey:@"name"];
    NSString *web_url = [defult objectForKey:@"picture"];
    if (name == nil) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"尚未登录" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
        UIAlertAction *login = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            RegistAndLoginViewController *vc = [[RegistAndLoginViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }];
        [alert addAction:login];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
        return;
//        name = @"txx";
//        web_url = @"123";
    }
    [manager insertReadCommentTableWithTableName:@"readComment" url:self.url content:_comment.text myID:@"000" type:@1 input_date:[self dateString] praisenum:@0 quote:nil touser_name:nil user_id:@"0000000" user_name:name web_url:web_url];

}
- (NSString *)dateString{
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSInteger length = textView.text.length + text.length;
    NSLog(@"%lu,%lu",range.location,range.length);
    _count.text = [NSString stringWithFormat:@"%ld",length];
    if (length <= ACCOUNT_MAX_CHARS) {
        NSInteger index;
        if (range.length == 0) {
            if (range.location == 0) {
                index = 0;
            }else{
                index = -1;
            }
            
        }else{
            index = 1;
        }
        _count.text = [NSString stringWithFormat:@"%ld",ACCOUNT_MAX_CHARS - range.location - range.length + index];
        return YES;
    }else{
        _count.text = @"0";
        return NO;
    }
    
}
-(void)textViewDidChange:(UITextView *)textView
{
    
    if (textView.text.length == 0) {
        _label.text = @"在这里写下你想说的...";
    }else{
        _label.text = @"";
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [_comment endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
